var express = require('express');
var router = express.Router();
const sql = require('../sql/sql')
const cart = require("../sql/cart");
const uuid = require("uuid")
const tools = require("../tools/index.js");
const pro = require("../sql/pro");
/* GET home page. */


/**
* @apiGroup 购物车信息
* @api {get} /api/cart/add 添加购物车商品数据
* @apiVersion 0.0.0
* @apiDescription 请求用户购物车商品数据
* @apiParam {String} userId 要添加购物车数据的用户id
* @apiParam {String} goodsId 要添加到购物车的商品id
* @apiParam {Number} num 数量
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "code":200,
*    "title":"请求成功",
*    "msg":[{},{},...]
* }
* @apiError {number} code 请求状态
* @apiError {string} title 请求说明
* @apiError {array} msg 请求数据
* @apiErrorExample {json} Error-Response:
  res.send({
      code:200,
      title:"首次添加购物车成功",
      msg:"ok"
  })
  res.send({
      code:201,
      title:"更新购物车成功",
      msg:"ok"
  })
  res.send({
      code:202,
      title:"商品不存在",
      msg:"no"
  })
* @apiSampleRequest http://localhost:3000/api/cart/add
*/


// 添加购物车数据的接口逻辑
// 1. 存什么数据, 提前创建集合对象
    // 购物车id：字符
    // 用户id：字符
    // 商品数据：数组
    //     [{proId,proName,price,img,num,flag},{...},{..}]
// 2. 接收数据↑
// 3. 验证登录状态
// 4. 读取商品数据，查看是否存在该商品
// 5. 是不是第一次加入购物车
    // 是，直接存，添加集合数据
    // 不是，判断是不是新商品
    //     是，添加商品数据，更新集合
    //     不是，修改数量，更新集合

router.post('/add', function(req, res, next) {
  let userId = req.body.userId;
  let goodsId = req.body.goodsId;
  let num = req.body.num;
  // console.log(req.query)
  let token = req.headers.token;
  // console.log(req.headers.token)
  
  tools.verify(token).then(result=>{
    // console.log(goodsId)
      sql.getData(pro, {goodsId}).then(pd=>{
          if(pd.length > 0){
              sql.getData(cart, {userId}).then(data=>{
                // console.log(data[0].proData)
                  if(data.length < 1){
                      sql.addData(cart, {
                          cartId: uuid.v1(),
                          userId: userId,
                          proData: [{
                              goodsId:goodsId,
                              num:num,
                              goodsImg:pd[0].goodsImg,
                              goodsName:pd[0].goodsName,
                              price:pd[0].price,
                              flag:1
                          }]
                      }).then(()=>{
                          res.send({
                              code:200,
                              title:"首次添加购物车成功",
                              msg:"ok"
                          })
                      })
                  }else{
                      let arr = data[0].proData
                      let i = 0;
                      let onoff = arr.some((val,idx)=>{
                          i = idx
                          return val.goodsId === goodsId
                      })
                      if(onoff){
                          arr[i].num += Number(num)
                      }else{
                          arr.push({
                              goodsId:goodsId,
                              num:num,
                              goodsImg:pd[0].goodsImg,
                              goodsName:pd[0].goodsName,
                              price:pd[0].price,
                              flag:1
                          })
                      }
                      sql.setData(cart, {userId}, {
                          proData: arr
                      }).then(()=>{
                          res.send({
                              code:201,
                              title:"更新购物车成功",
                              msg:"ok"
                          })
                      })
                  }
              })
          }else{
              res.send({
                  code:202,
                  title:"商品不存在",
                  msg:"no"
              })
          }
      })
  }).catch(err=>{
      res.send({
          code:400,
          title:"登录信息失效，请重新登录",
          msg:err
      })
  })
});






/**
* @apiGroup 购物车信息
* @api {get} /api/cart 获取购物车数据
* @apiVersion 0.0.0
* @apiDescription 获取购物车数据
* @apiParam {String} userId 要添加购物车数据的用户id
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
  res.send({
      code:200,
      title:"首次添加购物车成功",
      msg:data
  })
  res.send({
      code:400,
      title:"登录信息失效",
      msg:err
  })
* @apiSampleRequest http://localhost:3000/api/cart
*/

router.get('/', function(req, res, next) {
    let userId = req.query.userId;
    let token = req.headers.token;
    // console.log(9,token)
    tools.verify(token).then(result=>{
        sql.getData(cart, {userId}).then(data=>{
            let d = data[0] ? data[0] : {};
            res.send({
                code:200,
                title:"获取成功",
                msg:d
            })
        })
    }).catch(err=>{
        res.send({
            code:400,
            title:"登录失效",
            msg:err
        })
    })
})






/**
* @apiGroup 购物车信息
* @api {post} /api/cart/del 删除购物车数据
* @apiVersion 0.0.0
* @apiDescription 删除购物车数据
* @apiParam {String} userId 要删除购物车数据的用户id
* @apiParam {String} goodsId 要删除购物车数据的用户id
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
  res.send({
      code:200,
      title:"删除购物车成功",
      msg:data
  })
  res.send({
      code:400,
      title:"登录信息失效",
      msg:err
  })
* @apiSampleRequest http://localhost:3000/api/cart/del
*/

router.post('/del', function(req, res, next) {
    let userId = req.body.userId;
    let goodsId = req.body.goodsId;
    let token = req.headers.token;
    // console.log(9,token)
    tools.verify(token).then(result=>{
        sql.getData(cart, {userId}).then(data=>{
            // console.log(data[0].proData)
            if (data.length>0){
              let arr = data[0].proData;
              // console.log(arr)
              let i = 0;
              let off = arr.some((val,idx) => {
                i = idx;
                return val.goodsId === goodsId
              })
              if(off){
                arr.splice(i,1)
              }
              sql.setData(cart,{userId},{proData:arr}).then(()=>{
                sql.getData(cart,{userId}).then((cartData) => {
                  res.send({
                    code:200,
                    title:"删除成功",
                    msg:cartData[0]
                  })
                })
              })
            }else{
              res.send({
                  code:201,
                  title:"没有购物车数据",
                  msg:"no"
                })
              }
        })
    }).catch(err=>{
        res.send({
            code:400,
            title:"登录失效",
            msg:err
        })
    })
})


/**
* @apiGroup 购物车信息
* @api {post} /api/cart/set 修改购物车中的商品数量
* @apiVersion 0.0.0
* @apiDescription 修改购物车中的商品数量
* @apiParam {String} userId 要修改购物车数据的用户id
* @apiParam {String} goodsId 要修改购物车数据的商品id
* @apiParam {String} num 要修改购物车数据的商品的num
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
  res.send({
      code:200,
      title:"数量修改成功",
      msg:data
  })
  res.send({
      code:400,
      title:"登录信息失效",
      msg:err
  })
* @apiSampleRequest http://localhost:3000/api/cart/set
*/

router.post('/set', function(req, res, next) {
    let userId = req.body.userId;
    let goodsId = req.body.goodsId;
    let num = req.body.num;
    let token = req.headers.token;
    // console.log(9,token)
    tools.verify(token).then(result=>{
        sql.getData(cart, {userId}).then(data=>{
            // console.log(data[0].proData)
            if (data.length>0){
              let arr = data[0].proData;
              // console.log(arr)
              let i = 0;
              let off = arr.some((val,idx) => {
                i = idx;
                return val.goodsId === goodsId
              })
              if(off){
                arr[i].num = num
              }
              sql.setData(cart,{userId},{proData:arr}).then(()=>{
                sql.getData(cart,{userId}).then((cartData) => {

                  res.send({
                    code:200,
                    title:"数量修改成功",
                    msg:cartData
                  })
                })
              })
            }else{
              res.send({
                  code:201,
                  title:"没有购物车数据",
                  msg:"no"
                })
              }
        })
    }).catch(err=>{
        res.send({
            code:400,
            title:"登录失效",
            msg:err
        })
    })
})
/**
* @apiGroup 购物车信息
* @api {post} /api/cart/flag 修改购物车中的商品的选中状态
* @apiVersion 0.0.0
* @apiDescription 修改购物车中的商品的选中状态
* @apiParam {String} userId 要修改购物车数据的用户id
* @apiParam {String} goodsId 要修改购物车数据的商品id
* @apiParam {String} flag 要修改购物车数据的商品的flag
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
  res.send({
      code:200,
      title:"选中状态修改成功",
      msg:data
  })
  res.send({
      code:400,
      title:"登录信息失效",
      msg:err
  })
* @apiSampleRequest http://localhost:3000/api/cart/flag
*/

router.post('/flag', function(req, res, next) {
    let userId = req.body.userId;
    let goodsId = req.body.goodsId;
    let flag= req.body.flag;
    let token = req.headers.token;
    // console.log(9,token)
    tools.verify(token).then(result=>{
        sql.getData(cart, {userId}).then(data=>{
            // console.log(data[0].proData)
            if (data.length>0){
              let arr = data[0].proData;
              // console.log(arr)
              let i = 0;
              let off = arr.some((val,idx) => {
                i = idx;
                return val.goodsId === goodsId
              })
              if(off){
                arr[i].flag = flag
              }
              sql.setData(cart,{userId},{proData:arr}).then(()=>{
                sql.getData(cart,{userId}).then((cartData) => {

                  res.send({
                    code:200,
                    title:"选中状态修改成功",
                    msg:cartData
                  })
                })
              })
            }else{
              res.send({
                  code:201,
                  title:"没有购物车数据",
                  msg:"no"
                })
              }
        })
    }).catch(err=>{
        res.send({
            code:400,
            title:"登录失效",
            msg:err
        })
    })
})


/**
* @apiGroup 购物车信息
* @api {post} /api/cart/allflag 修改购物车中的所有商品的选中状态
* @apiVersion 0.0.0
* @apiDescription 修改购物车中的所有商品的选中状态
* @apiParam {String} userId 要修改购物车数据的用户id
* @apiParam {String} allflag 要修改购物车数据的商品的allflag
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
  res.send({
      code:200,
      title:"所有选中状态修改成功",
      msg:data
  })
  res.send({
      code:400,
      title:"登录信息失效",
      msg:err
  })
* @apiSampleRequest http://localhost:3000/api/cart/allflag
*/

router.post('/allflag', function(req, res, next) {
    let userId = req.body.userId;
    let flag= Number(req.body.flag);
    let token = req.headers.token;
    // console.log(req.body)
    // console.log(9,token)
    tools.verify(token).then(result=>{
        sql.getData(cart, {userId}).then(data=>{
            // console.log(data)
            if (data.length>0){
              let arr = data[0].proData;
              arr.forEach(val => {
                  val.flag = flag
              });
              sql.setData(cart,{userId},{proData:arr}).then(()=>{
                sql.getData(cart,{userId}).then((cartData) => {
                  res.send({
                    code:200,
                    title:"所有选中状态修改成功",
                    msg:cartData[0]
                  })
                })
              })
            }else{
              res.send({
                  code:201,
                  title:"没有购物车数据",
                  msg:"no"
                })
              }
        })
    }).catch(err=>{
        res.send({
            code:400,
            title:"登录失效",
            msg:err
        })
    })
})




module.exports = router;

